//
//  NSDate+Extension.m
//  ETong
//
//  Created by wanzhao on 15/11/25.
//  Copyright (c) 2015年 wanzhao. All rights reserved.
//

#import "NSDate+Extension.h"

@implementation NSDate (Extension)

+ (NSString*) getHourFromDate:(NSDate*)date {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    calendar.locale = [NSLocale currentLocale];
    calendar.timeZone = [NSTimeZone systemTimeZone];
    unsigned flags = NSHourCalendarUnit;
    NSDateComponents *components = [calendar components:flags fromDate:date];
    NSString *hourStr = [NSString stringWithFormat:@"%zd", [components hour]];
    return hourStr;
}

+ (NSString*) getMinuteFromDate:(NSDate*)date {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    calendar.locale = [NSLocale currentLocale];
    calendar.timeZone = [NSTimeZone systemTimeZone];
    unsigned flags = NSMinuteCalendarUnit;
    NSDateComponents *components = [calendar components:flags fromDate:date];
    NSString *minuteStr = [NSString stringWithFormat:@"%zd", [components minute]];
    return minuteStr;
}

+ (NSString*) getDayFromDate:(NSDate*)date {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    calendar.locale = [NSLocale currentLocale];
    calendar.timeZone = [NSTimeZone systemTimeZone];
    unsigned flags = NSDayCalendarUnit;
    NSDateComponents *components = [calendar components:flags fromDate:date];
    NSString *dayStr = [NSString stringWithFormat:@"%zd", [components day]];
    return dayStr;
}

+ (NSString*) getMonthFromDate:(NSDate*)date {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    calendar.locale = [NSLocale currentLocale];
    calendar.timeZone = [NSTimeZone systemTimeZone];
    unsigned flags = NSMonthCalendarUnit;
    NSDateComponents *components = [calendar components:flags fromDate:date];
    NSString *monthStr = [NSString stringWithFormat:@"%zd", [components month]];
    return monthStr;
}

+ (NSString*) getYearFromDate:(NSDate*)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    calendar.locale = [NSLocale currentLocale];
    calendar.timeZone = [NSTimeZone systemTimeZone];
    unsigned flags = NSYearCalendarUnit;
    NSDateComponents *components = [calendar components:flags fromDate:date];
    NSString *yearStr = [NSString stringWithFormat:@"%zd", [components year]];
    return yearStr;
}


+ (NSString*)dateStringForDate:(NSDate *)theDate
{
    NSCalendar *calen = [NSCalendar currentCalendar];
    calen.locale = [NSLocale currentLocale];
    calen.timeZone = [NSTimeZone systemTimeZone];

//    NSTimeInterval time = [[NSDate date] timeIntervalSince1970] * 1000 + [[USER_DEFAULT stringForKey:SERVER_TIME_SYS] doubleValue];
    
    
    
//    NSDate *localeDate = [NSDate dateWithTimeIntervalSince1970:time / 1000.0];
    
    NSString *timeString = nil;
    NSDate *localeDate = [NSDate date];
    //日历对象会将时间自动转为当前时区的时间
//    NSTimeZone *zone = [NSTimeZone systemTimeZone];
//    NSInteger interval = [zone secondsFromGMTForDate:localeDate];
//    NSDate *curDate = [localeDate dateByAddingTimeInterval: interval];
    //        NSLog(@"--%@",curDate);
    NSDateComponents *targetCmp = [calen components:NSCalendarUnitSecond | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:theDate];

    
    NSDateComponents *curDateCmp = [calen components:NSCalendarUnitSecond | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:localeDate];
    
    NSDateComponents *dateCmp = [[NSCalendar currentCalendar] components:NSCalendarUnitDay fromDateComponents:targetCmp toDateComponents:curDateCmp options:0];

       //当年
    if (dateCmp.day == 0) {
        //当月
        timeString = @"今天";
        
    }else if (dateCmp.day == 1){
        timeString = @"昨天";
    } else if (dateCmp.day == 2){
        timeString = @"前天";
    } else {
        timeString = [NSString stringWithFormat:@"%zd-%02zd-%02zd",targetCmp.year,targetCmp.month,targetCmp.day];
    }
    return timeString;
}

+ (NSString *)weekStringWithDate:(NSDate *)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    calendar.locale = [NSLocale currentLocale];
    calendar.timeZone = [NSTimeZone systemTimeZone];
    NSDateComponents *cmp = [calendar components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];
    NSArray *array = @[@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"];
    return [NSString stringWithFormat:@"%zd年%02zd月%02zd日  %@",cmp.year,cmp.month,cmp.day,array[cmp.weekday - 1]];
}

@end
